﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using TVR.Timerjobs.Utilitys;
using Microsoft.SharePoint.Utilities;
using System.Reflection;

namespace TVR.Timerjobs.JobDefenitions
{
    public class DummyJob : SPJobDefinition
    {
        private const string JobDefinitionTitle = "TVR.Timerjobs.JobDefenitions.DummyJob";
        public string dummyProperty = string.Empty;

        /// <summary>
        /// Initializes a new instance of the <see cref="HomePageRelevanteLinks"/> class.
        /// </summary>
        public DummyJob()
            : base()
        {
            this.Title = JobDefinitionTitle;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="HomePageRelevanteLinks"/> class.
        /// </summary>
        /// <param name="jobName">Name of the job.</param>
        /// <param name="service">The service.</param>
        /// <param name="server">The server.</param>
        /// <param name="targetType">Type of the target.</param>
        public DummyJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
        {
            this.Title = jobName;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GetLastPressReleases"/> class.
        /// </summary>
        /// <param name="jobName">Name of the job.</param>
        /// <param name="webApplication">The web application.</param>
        public DummyJob(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.Job)
        {
            this.Title = jobName;
        }




        public override void Execute(Guid targetInstanceId)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    Microsoft.SharePoint.Administration.SPAdministrationWebApplication centralWeb = SPAdministrationWebApplication.Local;
                    using (SPSite site = centralWeb.Sites["Central Administration"])
                    {
                        using (SPWeb web = site.RootWeb)
                        {
                            SPPropertyBag pBag = web.Properties;
                            Type jobType = this.GetType();
                            // Cycle through the properties.
                            foreach (FieldInfo varProperty in jobType.GetFields())
                            {
                                string propertyName = this.Title + "-" + varProperty.Name;
                                if (pBag.ContainsKey(propertyName))
                                {
                                    varProperty.SetValue(this, pBag[propertyName]);
                                }
                            }
                        }
                    }


                    Utility.WriteToSPLog("TIMERJOBTEST", dummyProperty);

                });
                base.Execute(targetInstanceId);
            }
            catch (Exception ex)
            {
                Utility.WriteToSPLog(ex.Source, ex.Message);

                //possible to build in - send mail or custom error action
            }

        }
    }
}
